#include "pool_rdwr.h"

void *WRITE_comm(void *arg)
{
    write(client_socket, "reg 1001", strlen("reg 1001")); // 发送数据
    return NULL;
}

// 管理员登入
bool RDWR_login(char *str)
{
    char sand_buf[1024] = {0};
    char rever_buf[1024] = {0};
    sprintf(sand_buf, "login,1001,%s", str);
    write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据

    pthread_mutex_lock(&LVGL_lock);       // 上锁
    read(client_socket, rever_buf, 1024); // 接收数据
    if (strstr(rever_buf, "ok") != NULL)
    {
        printf("登录成功\n");
        pthread_mutex_unlock(&LVGL_lock); // 解锁
        return true;
    }
    else
    {
        printf("登录失败\n");
        pthread_mutex_unlock(&LVGL_lock); // 解锁
        return false;
    }
}

void *REWR_read(void *arg)
{
    pthread_detach(pthread_self()); // pthread_self函数可以获取自己的线程id
    while (1)
    {
        // 接收信息
        char buf[1024] = {0};
        pthread_mutex_lock(&read_lock);
        int set = read(client_socket, buf, sizeof(buf)); // 从txt2_fd文件描述符中，读取128个字节的数据，放到read_buf里面
        if (set <= 0)
        {
            printf("服务端关闭了连接\n");
            break;
        }
        printf("从服务端收到的信息为 = %s\n", buf);
        if (strstr(buf, "take_off"))  //会有个消息框，提示商品下架
        {
            FILE *fd = fopen("./Data/commodity/Price_list.txt", "w");
            char rever_buf[128] = {0};
            while (1)
            {
                int set = read(client_socket, rever_buf, 1024); // 接收数据
                if (set < 0)
                {
                    printf("接收失败\n");
                }
                printf("%s\n", rever_buf);
                if (strstr(rever_buf, "end"))
                {
                    fflush(fd);
                    printf("获取成功\n");
                    break;
                }
                fwrite(rever_buf, 1, strlen(rever_buf), fd);
            }
            fclose(fd);
        }
        if (strstr(buf, "go_on")) // 接收图片
        {
            FILE *fd = fopen("./Data/commodity/Price_list.txt", "w");
            char rever_buf[128] = {0};
            while (1)
            {
                int set = read(client_socket, rever_buf, 1024); // 接收数据
                if (set < 0)
                {
                    printf("接收失败\n");
                }
                printf("%s\n", rever_buf);
                if (strstr(rever_buf, "end"))
                {
                    fflush(fd);
                    printf("获取成功\n");
                    pthread_exit(NULL);
                }
                fwrite(rever_buf, 1, strlen(rever_buf), fd);
            }
        }
        if(strstr(buf, "go_file"))
        {
            char h[128] = {0};
            char dev[128] = {0};
            char file[1024] = {0};
            char file_path[128] = {0};
            char *p1 = strtok(buf, ",");
            int i = 0;
            while (p1 != NULL)
            {
                printf("p1 = %s\n", p1);
                if (i == 1)
                    strcpy(h, p1);
                i++;
                p1 = strtok(NULL, ",");
            }
            sprintf(file, "touch /root/second_item/Data/commodity_picture/%s", h);
            sprintf(file_path, "/root/second_item/Data/commodity_picture/%s", h);
            printf("file = %s\n", file);
            system(file);
            tcp_file(file_path);
        }

        pthread_mutex_unlock(&read_lock);
        sleep(2);
    }
}

void file_rever(char *buf)
{
    char number[128] = {0};    //
    char file_name[128] = {0}; // 分析字符串
    char file_size[128] = {0};
    char file_dir[1024] = {0}; // 文件路径
    FILE_str(buf, number, file_name, file_size);
    sprintf(file_dir, "./Data/%s", number, file_name); // 拼接文件路径

    // b、打开文件(创建、可写)
    FILE *fp = fopen(file_dir, "w+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    while (1)
    {
        // 读取来自客户端的数据
        char file_buf[1024] = {0};
        int ret = read(client_socket, file_buf, sizeof(file_buf));
        if (ret <= 0)
        {
            break;
        }
        if (strstr(file_buf, "end"))
        {
            break;
        }
        // 将读取到的数据，写入到本地的文件中
        fwrite(file_buf, ret, 1, fp);
    }
}

int FILE_str(char *buf, char *number, char *file_name, char *file_size)
{
    char *p = strtok(buf, ",");
    int i = 0;
    while (p != NULL)
    {
        printf("p1 = %s\n", p);
        if (i == 0)
            ;
        else if (i == 1)
            strcpy(number, p);
        else if (i == 2)
            strcpy(file_name, p);
        else if (i == 3)
            strcpy(file_size, p);
        i++;
        p = strtok(NULL, ",");
    }
    return 0;
}
// 上传订单
int RDWR_order(pLinkList ptr)
{
    pLinkList p = ptr->next;
    pthread_mutex_lock(&read_lock); // 上锁
    while (p != NULL)
    {
        char sand_buf[1024] = {0};
        printf("order,1001,%s,%d\n", p->data, p->set_number);
        sprintf(sand_buf, "order,1001,%s,%d\n", p->data, p->set_number);
        write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据
        p = p->next;
        sleep(1);
    }
    sleep(1);
    write(client_socket, "order,1001,end", strlen("order,1001,end"));
    sleep(2);
    char rever_buf[1024] = {0};
    if (read(client_socket, rever_buf, 1024) < 0)
        ;
    {
        printf("读取失败\n");
    }
    printf("%s\n", rever_buf);
    printf("上传成功\n");
    if (strstr(rever_buf, "ok") != NULL)
    {
        printf("上传成功\n");
        pthread_mutex_unlock(&read_lock); // 解锁
        sleep(1);
        return 0;
    }

    else
    {
        printf("订单生成失败\n");
        pthread_mutex_unlock(&read_lock); // 解锁
        sleep(1);
        return -1;
    }
}

// 取消订单
int RDWR_Cancel()
{
    printf("取消订单\n");
    int set = write(client_socket, "order,1001,cancel", strlen("order,1001,cancel"));
    if (set < 0)
    {
        return -1;
    }
    return 0;
}

// 确定订单
int RDWR_int_order(int price)
{
    char sand_buf[255];
    sprintf(sand_buf, "order,1001,ok,%d", price);
    int set = write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据
    if (set < 0)
    {
        return -1;
    }
    return 0;
}

void * RDWR_get_new(char * rag)
{
    pthread_detach(pthread_self());
    char sand_buf[1024]  = {0};
    char rever_buf[1024] = {0};
    sprintf(sand_buf, "get_comm,1001");
    printf("获取商品信息\n");
    FILE *fd = fopen("./Data/commodity/Price_list.txt", "w");
    if(fd == NULL) {
        printf("文件打开失败\n");
        return NULL;
    }
    write(client_socket, sand_buf, strlen(sand_buf)); // 发送数据

    sleep(1);
    while(1) {
        int set = read(client_socket, rever_buf, 1024); // 接收数据
        if(set < 0) {
            printf("接收失败\n");
        }
        printf("%s\n", rever_buf);
        if(strstr(rever_buf, "end")) {
            fflush(fd);
            printf("获取成功\n");
            break;
        }
        fwrite(rever_buf, 1, strlen(rever_buf), fd);
    }
    fclose(fd);
    
}

int tcp_file(char *file_path)
{
    
    FILE *fp = fopen(file_path, "r+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        sleep(20);
        return -1;
    }
    while(1)
    {
        char buf[1024] = {0};
        int size = read(client_socket, buf, sizeof(buf));
        if (size <= 0)
        {
            printf("%s 设备断开连接\n");
            return -1;
        }
        if (strstr(buf, "end"))
        {
            break;
        }
        fwrite(buf, 1, strlen(buf), fp);
    }
    fclose(fp);
}